package com.example.hjlquickstartdemo.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.hjlquickstartdemo.api.UserApi;
import com.example.hjlquickstartdemo.constant.GlobalConstant;
import com.example.hjlquickstartdemo.dao.entity.User;
import com.example.hjlquickstartdemo.dao.mapper.UserMapper;
import com.example.hjlquickstartdemo.exception.AllErrorCode;
import com.example.hjlquickstartdemo.exception.BusinessException;
import com.example.hjlquickstartdemo.pojo.dto.IdVersion;
import com.example.hjlquickstartdemo.pojo.dto.UserDto;
import com.example.hjlquickstartdemo.pojo.query.UserQuery;
import com.example.hjlquickstartdemo.pojo.vo.CommonPage;
import com.example.hjlquickstartdemo.pojo.vo.ResultResponse;
import com.example.hjlquickstartdemo.service.UserService;
import com.example.hjlquickstartdemo.utils.ResultResponseUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;


@RestController
@AllArgsConstructor
public class UserController implements UserApi {
    /**
     * BasicUserService 包括了用户的所有的业务
     */
    @Autowired
    private final UserService userService;
    @Autowired
    private final UserMapper userMapper;


    @Override
    public ResultResponse<Boolean> add(UserDto userDto) {
        return ResultResponseUtils.booleanResult(userService.add(userDto));
    }

    @Override
    public ResultResponse<Boolean> remove(Long id) {
        return ResultResponseUtils.booleanResult(userService.remove(id));
    }

    @Override
    public ResultResponse<Boolean> remove(Long id, Long version) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(GlobalConstant.TableCommonField.ID_COLULMN_NAME, id).eq(GlobalConstant.TableCommonField.VERSION_COLUMN_NAME, version);
        return ResultResponseUtils.booleanResult(userMapper.delete(updateWrapper) > 0);
    }

    @Override
    public ResultResponse<Boolean> remove(IdVersion[] idVersions) {
        // 在事务中启用批量删除
        for (IdVersion idVersion : idVersions) {
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq(GlobalConstant.TableCommonField.ID_COLULMN_NAME, idVersion.getId())
                    .eq(GlobalConstant.TableCommonField.VERSION_COLUMN_NAME, idVersion.getVersion());
            if (userMapper.delete(updateWrapper) <= 0) {
                throw new BusinessException(AllErrorCode.DATA_DELETE_NOT_FOUND, "当前行的id和版本没匹配到记录");
            }
        }
        return ResultResponseUtils.booleanResult(true);
    }

    @Override
    public ResultResponse<Boolean> update(UserDto userDto) {
        return ResultResponseUtils.booleanResult(userService.update(userDto));
    }

    @Override
    public ResultResponse<CommonPage<UserDto>> search(UserQuery basicUserQuery) {
        return ResultResponseUtils.success(userService.search(basicUserQuery));
    }

    @Override
    public ResultResponse<UserDto> findById(Long id) {
        return ResultResponseUtils.success(userService.findById(id));

    }


//    /**
//     * 分页查询
//     * @param basicUserRT page 分页对象
//     * @param basicUserRT basicUser
//     * @return Object
//     */
//    @ApiOperation(value = "分页查询", notes = "分页查询")
//    @GetMapping("/page")
//    public ResultUtil<T> getBasicUserPage(@RequestBody RequestT<BasicUser> basicUserRT) {
//        return ResultUtil.success(basicUserService.page(basicUserRT.getPage(), Wrappers.query(basicUserRT.getBody())));
//    }

}
